package cn.datawin.cotask;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import cn.datawin.cotask.bo.CoTask;
import cn.datawin.cotask.util.CoTaskUtil;
import cn.datawin.cotask.util.PaserUtil;

public class TaskPoll implements Runnable {

	private String workbeach;
	private String state;
	private boolean stop = false;
	
	public static Queue<CoPage> queue = new LinkedList<CoPage>();
	
	public void offtask() throws Exception{
		List<Map<String, Object>> list = CoTaskUtil.getCoTask(workbeach, state);
		if(list==null || list.size()==0) return;
		List<CoTask> tasks = PaserUtil.dboToTask(list);
		for(CoTask task: tasks){
			CoPage page =  new CoPage(task);
			queue.offer(page);
		}
	}
	

	@Override
	public void run() {
		while(!stop){
			try{
				if(queue.size() == 0){
					offtask();
				}
			}catch(Exception e){
				e.printStackTrace();
				continue;
			}
		}
	}
	
	public String getWorkbeach() {
		return workbeach;
	}

	public void setWorkbeach(String workbeach) {
		this.workbeach = workbeach;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}

	public void start() {
		new Thread(this).start();
	}

}
